package com.plsjavaweb.core;

import org.apache.log4j.Logger;

import com.plsjavaweb.dao.DAOFactory;
import com.plsjavaweb.dao.interfaces.AccountDAO;
import com.plsjavaweb.logging.LoggingConstants;
//import com.pls.data.DataHelper;
//import com.pls.entity.EmployeeSeat;
//import com.pls.mapper.EmployeeSeatMapper;
import com.plsjavaweb.pojo.AccountPOJO;

public class AccountCore {
	static Logger log = Logger.getLogger(DAOFactory.class);
	static LoggingConstants logConst = new LoggingConstants();

	public void validateAccount(AccountPOJO account) {
		log.info(logConst.entryMessage);

		StringBuilder validationMessage = new StringBuilder();
		boolean hasCredentials = false;

		AccountPOJO accountDB = null;

		try {
			if ((account.getUsername().equals(null))
					|| (account.getUsername().equals(""))) {
				validationMessage.append("No username. ");
			} else if (((account.getPassword().equals(null)))
					|| (account.getPassword().equals(""))) {
				validationMessage.append("No password. ");
			} else if (((!account.getUsername().equals(null)))
					|| (!account.getUsername().equals(""))) {
				if (((!account.getPassword().equals(null)))
						|| (!account.getPassword().equals(""))) {
					hasCredentials = true;
				}
			}

			if (hasCredentials == true) {

				accountDB = new AccountPOJO();
				AccountDAO actDAO = DAOFactory.getAccountDAO();

				accountDB = actDAO.getAccountByUsername(account.getUsername());
				log.info(accountDB);
				if (accountDB != null) {
					if ((accountDB.getUsername().equals(account.getUsername()))
							&& (accountDB.getPassword().equals(account
									.getPassword()))) {
						account.setValid(true);
						account.setRole(accountDB.getRole());
						account.setEmployeeId(accountDB.getEmployeeId());
						account.setValidationMessage(validationMessage
								.toString());
					} else if ((accountDB.getUsername().equals(account
							.getUsername()))
							&& (!accountDB.getPassword().equals(
									account.getPassword()))) {
						account.setValid(false);
						validationMessage.append("Invalid password. ");
						account.setValidationMessage(validationMessage
								.toString());
					}
				} else {
					account.setValid(false);
					validationMessage.append("Username does not exist");
					account.setValidationMessage(validationMessage.toString());
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			log.info(logConst.errorMessage);
		}

		log.info(logConst.exitMessage);
	}

}
